package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *
 */
@Service
public class HangReasonAttrHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        String lockOrReleaseOpType = querySceneInstService.getExtParameterValue(sceneInstId, "lockOrReleaseOpType");
        // 挂起时限
        KiteAttr hangDateAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10279");
        // A端要求完成时间/PE端要求完成时间
        KiteAttr aFinishAttr = querySceneInstService.getCommonAttr(sceneInstId, "2_0000122");
        // Z端要求完成时间/CE端要求完成时间
        KiteAttr zFinishAttr = querySceneInstService.getCommonAttr(sceneInstId, "2_0000123");
        // 全程要求完成时间
        KiteAttr allFinishAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10012");

        if (KiteStringUtils.equals("LOCK", lockOrReleaseOpType)) {
            if(Objects.nonNull(aFinishAttr)){
                aFinishAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                aFinishAttr.setIsNull(KeyConsts.IFTRUE_T);
                operateSceneCommonService.setCommonAttr(sceneInstId, aFinishAttr);
            }
            if(Objects.nonNull(zFinishAttr)){
                zFinishAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                zFinishAttr.setIsNull(KeyConsts.IFTRUE_T);
                operateSceneCommonService.setCommonAttr(sceneInstId, zFinishAttr);
            }
            if(Objects.nonNull(allFinishAttr)){
                allFinishAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                allFinishAttr.setIsNull(KeyConsts.IFTRUE_T);
                operateSceneCommonService.setCommonAttr(sceneInstId, allFinishAttr);
            }
        } else if (KiteStringUtils.equals("RELEASE", lockOrReleaseOpType)) {
            if(Objects.nonNull(hangDateAttr)){
                hangDateAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                hangDateAttr.setIsNull(KeyConsts.IFTRUE_T);
                operateSceneCommonService.setCommonAttr(sceneInstId, hangDateAttr);
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
